/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - resource
// Copyright( c) 2014.  All Rights Reserved
//
// File:		AETextureManager.cpp
// Author:		Gianluca Belardelli
// Date:		02/10/2014
//
/////////////////////////////////////////////////////////////////////////
#include <AEEngine.h>

AETextureManager *AETextureManager::g_lpGlobalManager = 0L;

AETextureManager::AETextureManager( const char *lpName ) : AEResourceManager( lpName )
{
	m_nProviderCount=0;
}

AEResource *AETextureManager::CreateResource( const char *lpFilename, AEINT32 nFlags )
{
	return 0L;
}


AETexture *AETextureManager::CreateRenderableTexture( const char *lpName, AERenderableTextureConfig &rtConfig, AEUINT32 uiLoadingFlags )
{
	if( !lpName || strlen(lpName ) == 0 )
		return AENULL;

	AERenderableTexture *lpTexture = new AERenderableTexture( this, rtConfig );
	lpTexture->Load( lpName );
	AddResource( lpTexture );

	return lpTexture;
}

AETexture *AETextureManager::Load2DTextureFromFile( const char *lpFilename, AEINT32 nFlags )
{
	AEASSERT( m_nProviderCount > 0 );

	const char *lpExt = AEStringUtils::GetExtension( lpFilename );
	if( lpExt == 0L )
		return 0L;

	AETextureLoaderI *lpLoader;

	for( AEINT32 i=0; i<m_nProviderCount; i++ )
	{
		AEINT32 nCount;
		const char **lpSupportedExt = m_lpLoader[i]->GetSupportedFileExtensions( nCount );

		for( AEINT32 n=0; n<nCount; n++ )
		{
			if( !strcmp( lpSupportedExt[n], lpExt ) )
			{
				lpLoader = m_lpLoader[i];
				break;
			}

		}
	}

	if( lpLoader )
	{
		AETexture *lpTexture = new AETexture( this );
		lpTexture->SetResourceFlag( nFlags );
		if( lpTexture->Load( lpFilename ) )
		{
			AddResource( lpTexture );
			return lpTexture;
		}
	}

	return 0L;
}

AETexture *AETextureManager::Load3DTextureFromFile( const char *lpFilename, AEINT32 nFlags )
{
	return 0L;
}

#if defined(AE_SUPPORTS_2D_TEXTURE_ARRAYS)

AETexture *AETextureManager::Load2DArrayTextureFromFile( const char *lpFilename, AEINT32 nFlags )
{
	return 0L;
}

#endif

AETexture *AETextureManager::LoadCubemapTextureFromFile( const char *lpFilename, AEINT32 nFlags )
{
	return 0L;
}

AETexture *AETextureManager::GetRenderableCubemap( const char *lpKey, AEBOOL32 bForceCreate )
{
	return 0L;
}

void AETextureManager::TriggerFileNotFound( const char *lpFilename )
{
}

void AETextureManager::TriggerLoadingError( const char *lpFilename, const char *lpErrorMsg )
{
}

void AETextureManager::UnbindTexture( AETexture *lpTexture )
{
}

AETextureLoaderI *AETextureManager::GetLoaderForExtension( const char *lpExt )
{
	for( AEINT32 i=0; i<m_nProviderCount; i++ )
	{
		AEINT32 nCount;
		const char **lpSupportedExt = m_lpLoader[i]->GetSupportedFileExtensions( nCount );

		for( AEINT32 n=0; n<nCount; n++ )
		{
			if( !strcmp( lpSupportedExt[n], lpExt ) )
				return m_lpLoader[i];
		}
	}

	return 0L;
}
